package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import org.jetbrains.annotations.NotNull;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 房间标签数据列表
 */
public class SelectFloorAdapter extends AppAdapter<FloorBean> {
    private FloorBean mSelected = null;
    private boolean canBeEmpty = false;
    private boolean isSingleSelect = false;
    public SelectFloorAdapter(Context context) {
        super(context);
    }

    public SelectFloorAdapter(@NonNull @NotNull Context context, boolean canBeEmpty, boolean isSingleSelect) {
        super(context);
        this.canBeEmpty = canBeEmpty;
        this.isSingleSelect = isSingleSelect;
    }

    public FloorBean getSelectedData(){
        return mSelected;
    }

    public void initSelectMap(FloorBean data){
        mSelected = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView floorName;
        private final CheckBox checkbox;

        private ViewHolder() {
            super(R.layout.select_floor_item1);
            floorName = findViewById(R.id.floor_name);
            checkbox = (CheckBox) findViewById(R.id.checkbox);
        }

        @Override
        public void onBindView(int position) {
            FloorBean floor = getItem(position);
            checkbox.setTag(getItem(position));
            checkbox.setOnClickListener(mCheckListener);
            floorName.setText(floor.getFloorName());
            if (mSelected != null && mSelected.getFloorSerialNo().equals(floor.getFloorSerialNo())){
                checkbox.setChecked(true);
            }else {
                checkbox.setChecked(false);
            }
        }
    }

    private View.OnClickListener mCheckListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FloorBean floor = (FloorBean) v.getTag();
            if (((CheckBox)v).isChecked()){
                mSelected = floor;
                notifyDataSetChanged();
                onSelectChanged(mSelected);
            }else {
                ((CheckBox)v).setChecked(true);
            }
        }
    };

    public void setSelected(FloorBean selected) {
        this.mSelected = selected;
    }

    public void clearSelected() {
        mSelected = null;
    }

    public void onSelectChanged(FloorBean selected) {
    }
}